package com.coocaa.ops.admin.v1.todotask;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.todotask.model.AuditTaskInfoModel;
import com.coocaa.ops.admin.v1.todotask.model.TaskPageListModel;
import com.coocaa.ops.admin.v1.todotask.provider.ITaskQueryProvider;
import com.coocaa.ops.admin.v1.todotask.service.IDraftService;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hql
 * @date 2020/3/26
 */
@RestController
@RequestMapping("/v1")
public class TodoTaskController extends MgrBaseController {
    @Autowired
    private IDraftService draftService;
    @Autowired
    private ITaskQueryProvider taskQueryProvider;

    /**
     * 我的草稿 分页查询
     *
     * @param request
     * @param response
     * @param model
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/myDrafts/pageList",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String draftPageList(HttpServletRequest request, HttpServletResponse response, AuditTaskInfoModel model) throws Exception {
        Assert.hasText(model.getResourceType(), "资源类型不能为空,resourceType");
        SysUserEntity user = WebUtil.getCurrentUser(request);
        if (user == null || user.getId() == null) {
            return null;
        }
        Map<String, Object> result =  draftService.listDraftPageResult(model, user.getId());
        return render2Json(result);
    }

    /**
     * 我的草稿 获取总数
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/myDrafts/getTotal",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String getTotalOfMyDrafts(HttpServletRequest request) {
        SysUserEntity user = WebUtil.getCurrentUser(request);

        if (user == null || user.getId() == null) {
            return null;
        }
        int total = draftService.getDraftTotalCount(user.getId());
        return Integer.toString(total);
    }

    /**
     * 我提交的任务 分页查询
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/mySubmittedTasks/pageList", method = {RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    public String submittedTaskPageList(HttpServletRequest request, AuditTaskInfoModel model) throws Exception {
        Assert.hasText(model.getResourceType(), "资源类型不能为空,resourceType");
        SysUserEntity user = WebUtil.getCurrentUser(request);
        if (user == null || user.getId() == null) {
            return null;
        }
        model.setSubmitTaskIs(true);
        PageResultModel<TaskPageListModel> pageData = taskQueryProvider.listTaskPage(model, user.getId());
        Map<String, Object> jsonMap = R.ok(pageData);
        return render2Json(jsonMap);
    }

    /**
     * 获取我提交任务的待审核和审核中总数
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/mySubmittedTasks/getTotal", method = {RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    public String getTotalOfMySubmittedTasks(HttpServletRequest request) {
        SysUserEntity user = WebUtil.getCurrentUser(request);
        if (user == null || user.getId() == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", user.getId());
        int total = taskQueryProvider.getTotalTask(map);
        return Integer.toString(total);
    }

    /**
     * 我的审核任务 分页查询
     *
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/myAuditTasks/pageList",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String pageList(HttpServletRequest request, AuditTaskInfoModel model) throws Exception {
        Assert.hasText(model.getResourceType(), "资源类型不能为空,resourceType");
        SysUserEntity user = WebUtil.getCurrentUser(request);
        if (user == null || user.getId() == null) {
            return null;
        }
        model.setSubmitTaskIs(false);
        PageResultModel<TaskPageListModel> pageData = taskQueryProvider.listTaskPage(model, user.getId());
        Map<String, Object> jsonMap = R.ok(pageData);
        return render2Json(jsonMap);
    }

    /**
     * 获取我审核中的任务总数
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/myAuditTasks/getTotal",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String getTotalOfMyAuditTasks(HttpServletRequest request) {
        SysUserEntity user = WebUtil.getCurrentUser(request);
        if (user == null || user.getId() == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("currentOperator", user.getId());
        map.put("auditFlag", 3);
        int total = taskQueryProvider.getTotalTask(map);
        return Integer.toString(total);
    }
}
